/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class CutString extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String source;
	private int subLength = 0;
	private String appendString = "";

	public String getAppendString() {
		return this.appendString;
	}

	public void setAppendString(String appendString) {
		this.appendString = appendString;
	}

	public String getSource() {
		return this.source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public int getSubLength() {
		return this.subLength;
	}

	public void setSubLength(int subLength) {
		this.subLength = subLength;
	}

	public int doStartTag() throws JspException {
		try {
			JspWriter e = this.pageContext.getOut();
			String str = "";
			if (this.source != null && !this.source.equals("")) {
				str = this.source;
				if (this.subLength > 0) {
					str = this.doCut(str, 0, this.subLength);
				}
			}

			e.print(str);
			return 0;
		} catch (Exception arg2) {
			throw new JspException(arg2.toString());
		}
	}

	public int doEndTag() {
		return 6;
	}

	private String doCut(String s, int start, int length) throws Exception {
		int cuted = 0;
		String s1 = "";

		for (int k = start; k < s.length(); ++k) {
			String s2 = s.substring(k, k + 1);
			byte[] abyte0 = s2.getBytes("GBK");
			if (abyte0.length != 2 && !s2.equals("m") && !s2.equals("M")
					&& !s2.equals("W")) {
				++cuted;
			} else {
				cuted += 2;
				if (cuted > length) {
					s1 = s1 + this.appendString;
					break;
				}
			}

			s1 = s1 + s2;
			if (cuted == length) {
				if (k < s.length() - 1) {
					s1 = s1 + this.appendString;
				}
				break;
			}
		}

		return s1;
	}
}